package com.example.administrator.szb.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bumptech.glide.Glide;
import com.example.administrator.szb.R;
import com.example.administrator.szb.adapter.ViewPagerAdapter_forYDY;
import com.example.administrator.szb.tinkerutil.SampleApplicationLike;
import com.example.administrator.szb.http.HttpUtils;
import com.example.administrator.szb.http.OnResultListener;
import com.example.administrator.szb.http.URLAddress;
import com.example.administrator.szb.util.AppUtils;
import com.example.administrator.szb.util.SPUtils;
import com.meiqia.core.MQManager;
import com.meiqia.core.bean.MQAgent;
import com.meiqia.core.bean.MQMessage;
import com.meiqia.core.callback.OnClientOnlineCallback;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by D N on 2017/9/14.
 */

public class WecomeActivity  extends Activity {
    private ViewPager wecomeViewpage;
    public static MQManager mqManager;
    private int isShowYDY = 0;//是否显示欢迎页，0，未加载过，1加载过
    private ImageView iv_wecome;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        setContentView(R.layout.activity_wecome);
        SampleApplicationLike.FLAGS=1;
        initView();
        initEvent();
        initData();
        getLoadPage();
        //JPushInterface.setAlias(this, 0, SampleApplicationLike.getUserloginInstance().getId() + "");
    }

    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (isShowYDY == 1){
                if (urls!=null){
                    Intent intent = new Intent(WecomeActivity.this,Welcome2.class);
                    intent.putExtra("url",urls);
                    startActivity(intent);
                    finish();
                    return;
                }
                startActivity(new Intent(WecomeActivity.this, HomeActivity.class));
                iv_wecome.setImageBitmap(null);
                wecomeViewpage = null;
                finish();
            }

        }
    };
    protected void initView() {
        AppUtils.isClearData();

        isShowYDY = SPUtils.getDatasInt(SPUtils.IS_LOADED_YDY);
        wecomeViewpage = findViewById(R.id.wecome_viewpage);
        List<View> views = new ArrayList<View>();
        LinearLayout.LayoutParams mParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        if (isShowYDY == 1){
            iv_wecome = new ImageView(this);
            iv_wecome.setLayoutParams(mParams);
            Glide.with(this).load(R.mipmap.wecome2).into(iv_wecome);
            //iv_wecome.setImageResource(R.drawable.wecome2);
            iv_wecome.setScaleType(ImageView.ScaleType.CENTER_CROP);
            views.add(iv_wecome);
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    handler.sendEmptyMessage(0);
                }
            },1500);
        }else{
            final ImageView iv_wecome_one = new ImageView(this);
            iv_wecome_one.setLayoutParams(mParams);
            Glide.with(this).load(R.mipmap.ydy_one_new).into(iv_wecome_one);
            //iv_wecome_one.setImageResource(R.mipmap.ydy_one);
            iv_wecome_one.setScaleType(ImageView.ScaleType.CENTER_CROP);

            final ImageView iv_wecome_two = new ImageView(this);
            iv_wecome_two.setLayoutParams(mParams);
            Glide.with(this).load(R.mipmap.ydy_two_new).into(iv_wecome_two);
            //iv_wecome_two.setImageResource(R.mipmap.ydy_two);
            iv_wecome_two.setScaleType(ImageView.ScaleType.CENTER_CROP);

            final ImageView iv_wecome_one2 = new ImageView(this);
            iv_wecome_one2.setLayoutParams(mParams);
            Glide.with(this).load(R.mipmap.ydy_last_new).into(iv_wecome_one2);
            //iv_wecome_one.setImageResource(R.mipmap.ydy_one);
            iv_wecome_one2.setScaleType(ImageView.ScaleType.CENTER_CROP);

            final View iv_wecome_four = LayoutInflater.from(this).inflate(R.layout.webcome_ydy,null);
            //((ImageView)iv_wecome_four.findViewById(R.id.imageView3)).setImageResource(R.mipmap.ydy_four);
            Glide.with(this).load(R.mipmap.ydy_four_new).into(((ImageView)iv_wecome_four.findViewById(R.id.imageView3)));
            iv_wecome_four.setLayoutParams(mParams);
            //iv_wecome_four.setImageResource(R.drawable.ydy_four);
            //iv_wecome_four.setScaleType(ImageView.ScaleType.FIT_XY);
            iv_wecome_four.findViewById(R.id.button6).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    iv_wecome_one.setImageBitmap(null);
                    iv_wecome_two.setImageBitmap(null);
                    ((ImageView)iv_wecome_four.findViewById(R.id.imageView3)).setImageBitmap(null);
                    wecomeViewpage = null;
                    startActivity(new Intent(WecomeActivity.this, HomeActivity.class));
                    finish();
                }
            });

            views.add(iv_wecome_one);
            views.add(iv_wecome_two);
            views.add(iv_wecome_one2);

            views.add(iv_wecome_four);

            SPUtils.setDatas(SPUtils.IS_LOADED_YDY,1);
        }
        ViewPagerAdapter_forYDY vpAdapter = new ViewPagerAdapter_forYDY(views);
        wecomeViewpage.setAdapter(vpAdapter);
    }


    protected void initEvent() {
        mqManager = MQManager.getInstance(this);
        //mqManager.setCurrentClient();
        mqManager.setCurrentClientOnline(new OnClientOnlineCallback() {
            @Override
            public void onFailure(int i, String s) {

            }

            @Override
            public void onSuccess(MQAgent mqAgent, String s, List<MQMessage> list) {

            }
        });

    }


    protected void initData() {

    }
    String urls = null;
    private void getLoadPage(){
        HttpUtils.post(this,URLAddress.LoadPage, new HashMap<String, Object>(), new OnResultListener() {
            @Override
            public void onSuccess(int requestCode, String str) {
                if (!TextUtils.isEmpty(str)) {
                    urls = str;
                }
            }

            @Override
            public void onFailure(int requestCode, String str) {

            }
        });
    }
}
